package yl.hs.bmipfull.controllers.material;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import yl.hs.bmipfull.controllers.ControllerBase;
import yl.hs.bmipfull.pojo.material.PurchaseContractProductEntity;
import yl.hs.bmipfull.pojo.salemanage.SaleContractEntity;
import yl.hs.bmipfull.service.material.IPurchaseContractService;
import yl.hs.bmipfull.service.system.ISearchViewService;
import yl.hs.bmipfull.utils.HsApp;
import yl.hs.bmipfull.utils.Tl;
import yl.hs.bmipfull.utils.datastate.ActionType;
import yl.hs.bmipfull.utils.datastate.ContractState;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.SubmitModel;
import yl.hs.bmipfull.viewmodels.WebResult;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 作用:  采购合同信息控制层<br/>
 * 创建时间: 2021年05月19日 15时30分07秒 <br/>
 *
 * @author 吴高丰
 */
@Api(value = "采购合同信息Web接口", tags = "采购管理——采购合同信息操作")
@RequestMapping(value = "/api/purchase/contract")
@RestController
public class PurchaseContractController extends ControllerBase {

    @Autowired
    IPurchaseContractService service;
    @Autowired
    ISearchViewService svSVC;
    @ApiOperation(value = "查询采购合同信息", notes = "查询采购合同信息")
    @RequestMapping(value = "", method = RequestMethod.POST)
    public WebResult queryList(@ApiParam(value = "查询条件") @RequestBody PageQuery pageQuery) {
        try {
            return success("查询成功", service.queryList(pageQuery));
        } catch (Exception e) {
            return error(e);
        }
    }

    @ApiOperation(value = "查询采购合同信息", notes = "分页查询采购合同信息")
    @RequestMapping(value = "/{rowNum}/{page}", method = RequestMethod.POST)
    public WebResult queryPage(@RequestBody PageQuery query,
                               @ApiParam(value = "页码") @PathVariable int page,
                               @ApiParam(value = "每页条数") @PathVariable int rowNum) {
        try {
            var result = service.queryPage(query, rowNum, page);
            svSVC.updateUserMenuView(query);
            return success("ok", result);
        } catch (Exception ee) {
            return error(ee);
        }
    }


    @ApiOperation(value = "查询采购合同信息", notes = "根据采购合同信息编号查询采购合同信息")
    @RequestMapping(value = "/{sn}", method = RequestMethod.GET)
    public WebResult findBySn(@ApiParam(value = "编号") @PathVariable String sn) {
        return success("查询成功", service.findBySn(sn));
    }


    @ApiOperation(value = "保存采购合同信息", notes = "查询/更新采购合同信息信息")
    @RequestMapping(value = "", method = RequestMethod.PUT)
    public WebResult saveMenu(@ApiParam(value = "提交信息") @RequestBody SubmitModel submitModel) {
        Integer result;
        try {
            List<Map<String, Object>> datas = (List<Map<String, Object>>) submitModel.getData().get("formItem");
            List<PurchaseContractProductEntity> itemList = new ArrayList<>();
            for (var item : datas) {
                PurchaseContractProductEntity entity = new PurchaseContractProductEntity();
                Tl.getChangeLog(entity, item);
                itemList.add(entity);
            }
            result = service.savePurchaseContract(submitModel.getData(), submitModel.getSn(), itemList);
            if (result > 0) {
                return success("保存成功");
            } else {
                return fail("数据未保存");
            }
        } catch (Exception e) {
            return error(e);
        }

    }

    @ApiOperation(value = "删除采购合同信息信息", notes = "根据采购合同信息编号删除系采购合同信息信息")
    @RequestMapping(path = "", method = RequestMethod.DELETE)
    public WebResult delete(@ApiParam(value = "采购合同信息编号", name = "sn", allowMultiple = true, required = true) @RequestParam("sn") String[] sn) {
        if (service.delete(sn) > 0) {
            return success("删除成功");
        } else {
            return fail("删除失败");
        }

    }
    @ApiOperation(value = "根据编号修改状态", notes = "根据编号修改状态")
    @RequestMapping(value = "/changeStateBySn/{sn}/{state}", method = RequestMethod.GET)
    @ResponseBody
    public WebResult changeStateBySn(@PathVariable String sn,@PathVariable short state) {
        try {
            Integer result = service.changeStateBySn(sn.split(","), state);
            if (result > 0) {
                return success("数据已修改");
            } else if (result < -2) {
                return fail("数据修改失败！");
            }
            return fail("没有修改的任何数据");
        } catch (Exception ee) {
            return error(ee);
        }
    }
    @ApiOperation(value = "暂停合同", notes = "暂停合同")
    @RequestMapping(value = "Suspend/{sn}/{remark}", method = RequestMethod.POST)
    @ResponseBody
    public WebResult Suspend(@PathVariable String sn,@PathVariable String remark) {
        try {
            var user= HsApp.getCurrent();
            Integer result = service.changeStateBySn(sn.split(","), ContractState.Suspend.getCode() );
            service.writeLog(ActionType.UPDATE,sn,user.getUserName()+"暂停了合同；暂停理由："+remark,"", SaleContractEntity.class);
            if (result > 0) {
                return success("数据已修改");
            } else if (result == -2) {
                return fail("数据修改失败！");
            }
            return fail("没有修改的任何数据");
        } catch (Exception ee) {
            return error(ee);
        }
    }
    @ApiOperation(value = "结束合同", notes = "结束合同")
    @RequestMapping(value = "End/{sn}/{remark}", method = RequestMethod.POST)
    @ResponseBody
    public WebResult End(@PathVariable String sn,@PathVariable String remark) {
        try {
            var user= HsApp.getCurrent();
            Integer result = service.changeStateBySn(sn.split(","), ContractState.End.getCode());
            service.writeLog(ActionType.UPDATE,sn,user.getUserName()+"结束了合同；结束理由："+remark,"", SaleContractEntity.class);
            if (result > 0) {
                return success("数据已修改");
            } else if (result == -2) {
                return fail("数据修改失败！");
            }
            return fail("没有修改的任何数据");
        } catch (Exception ee) {
            return error(ee);
        }
    }
    @ApiOperation(value = "重新启动合同", notes = "重新启动合同")
    @RequestMapping(value = "Restart/{sn}/{remark}", method = RequestMethod.POST)
    @ResponseBody
    public WebResult Restart(@PathVariable String sn,@PathVariable String remark) {
        try {
            var user= HsApp.getCurrent();
            Integer result = service.changeStateBySn(sn.split(","), ContractState.Signed.getCode());
            service.writeLog(ActionType.UPDATE,sn,user.getUserName()+"重新启动了合同；启动原因："+remark,"", SaleContractEntity.class);
            if (result > 0) {
                return success("数据已修改");
            } else if (result == -2) {
                return fail("数据修改失败！");
            }
            return fail("没有修改的任何数据");
        } catch (Exception ee) {
            return error(ee);
        }
    }
}